package de.muc.vanbeek.logging;

import org.slf4j.Logger;

/**
 * This does not add much functionality, but removes the need to set the class name in the
 * {@link org.slf4j.LoggerFactory} call. But maybe an indirection between our code and the
 * {@link org.slf4j.LoggerFactory} is enough motivation for the existence of this class.
 * Usage:
 * 
 * <code>
 * private static final Logger sLogger = LoggerFactory.create();
 * </code>
 * 
 * Please make the logger a static variable always, because instantiating it as an instance
 * variable will automatically prevent any logging method to become static.
 */
public final class LoggerFactory {

	 /**
     * This is the index in the stack trace of the method that called us, if we determine it via
     * {@link Thread#currentThread()#getStackTrace()}.
     */
    public static final int DIRECT_CALLER_STACK_TRACE_INDEX = 2;
    
    /**
     * Private constructor since there should be no instances of this class.
     */
    private LoggerFactory() {
        // nothing to do here
    }
    
    /**
     * Creates a commons logging Log instance. The logger instance is initialized with the class calling
     * this method.
     * 
     * @return A {@link Logger} instance with the name that is set to the class name of our caller.
     */
    public static Logger create() {
        StackTraceElement directCaller = Thread.currentThread().getStackTrace()[DIRECT_CALLER_STACK_TRACE_INDEX];
        return org.slf4j.LoggerFactory.getLogger(directCaller.getClassName());
    }
}
